﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuakeStats
{
    class ClientInfoEntry : LogEntry
    {
        public ClientInfoEntry(string line) : base(line)
        {}

        private Dictionary<string, string> _dataValues;
        Player _player;

        protected override void ParseLineData(string line)
        {

            //  1:30 ClientUserinfoChanged: 0 n\Umfundisii\t\3\model\visor/gorre\hmodel\visor/gorre\g_redteam\Stroggs\g_blueteam\Pagans\c1\4\c2\5\hc\100\w\0\l\0\tt\0\tl\0
            ClientId = line.Trim().Split(':')[2].Trim().Split(' ')[0];

            string data = line.Trim().Split(':')[2].Trim();

            int x = 0;
            char isSpace = data[x];

            while (isSpace != ' ')
            {
                x++;
                isSpace = data[x];
            }
            data = data.Substring(x + 1);
            
            _dataValues = CreateDictionary(data);

            InitDbPlayer();
        }

        private void InitDbPlayer()
        {
            _player = SingleContext.Instance.Players.Where(p => p.Name == this.Name).FirstOrDefault();
            if (_player == null)
            {
                _player = new Player();
                _player.Name = Name;
                SingleContext.Instance.AddToPlayers(_player);
                SingleContext.Instance.SaveChanges();
            }
        }

        public string Name { get { return _dataValues["n"]; } }
        public string Team { get { return _dataValues["t"]; } }
        public string Model { get { return _dataValues["model"]; }  }
        public string ClientId {get; set;}
        public Player DbPlayer {get { return _player; } } 
    }
}
